﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SYH.Public.BLL
{
    public class Public_MessagesReceivers
    {
        public static readonly SYH.Public.DAL.Public_MessagesReceivers dal = new DAL.Public_MessagesReceivers();
        
        #region 查找收件
        /// <summary>
        /// 根据id查找收件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.Public_MessagesReceivers GetReceiverById(string id)
        {
            return dal.GetReceiverById(id);
        }
        #endregion

        #region 收件箱中总消息或未读消息数
        /// <summary>
        /// 收件箱中总消息或未读消息数
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="IsTotal">true 收件箱总数。false 未读信息数</param>
        /// <param name="TypeId">0是非系统消息，1是系统消息</param>
        /// <returns></returns>
        public int GetTotalReceivedMessage(int UserId, bool IsTotal, int TypeId)
        {
            return dal.GetTotalReceivedMessage( UserId,  IsTotal,  TypeId);
        }
        #endregion

        #region 批量删除收件箱
        /// <summary>
        /// 批量删除收件箱
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static int DeleteReceiverList(string idList)
        {
            return dal.DeleteReceiverList(idList);
        }
        #endregion

        #region  收件箱
        /// <summary>
        /// 分页收件箱列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="recordCount"></param>
        /// <param name="userId"></param>
        /// <param name="typeStyle ">0是系统消息，1是收件箱消息</param>
        /// <param name="sqlWhere"></param>
        /// <returns></returns>
        public static DataSet GetReceiverList(int pageSize, int pageIndex, out int pageCount, out int recordCount, int userId, int typeStyle, string sqlWhere)
        {
            return dal.GetReceiverList( pageSize,  pageIndex, out  pageCount, out  recordCount,  userId,  typeStyle,  sqlWhere);
        }
        #endregion

        #region 根据id集合批量修改为已读消息
        /// <summary>
        /// 根据id集合批量修改为已读消息
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public void UpdateReaderList(string idList)
        {
            dal.UpdateReaderList(idList);
        }
        #endregion

        #region  收件箱根据mailid查找发件人
        /// <summary>
        /// 根据mailid查找发件人
        /// </summary>
        /// <param name="mailId"></param>
        /// <returns></returns>
        public DataSet SearchSender(int mailId)
        {
            return dal.SearchSender(mailId); ;
        }
        #endregion

        #region 根据id修改为已读消息
        /// <summary>
        /// 根据id修改为已读消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateIsReader(string id)
        {
            return dal.UpdateIsReader(id);
        }
        #endregion

        public void UpdateIsReadered(string[] Str)
        {
            if (Str.Length > 0)
            {
                string idList = "";
                foreach (string id in Str)
                {
                    idList += id + ",";
                }
                idList = idList.Substring(0, idList.Length - 1);
                if (!string.IsNullOrEmpty(idList))
                {
                   UpdateReaderList(idList);
                }
            }
        }

        /// <summary>
        /// 收件人
        /// </summary>
        /// <param name="oMailId"></param>
        /// <returns></returns>
        public static string GetReceivers(int oMailId)
        {
            string result = "";
            int mailId = oMailId;
            DataSet ds = SYH.Public.BLL.Public_MessagesSenders.SearchReceivers(mailId);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                result +=  ds.Tables[0].Rows[i][0] ;
            }
            return result;
        }

        #region 获取发件人信息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mailId"></param>
        /// <returns></returns>
        public static string GetReceiverInfo(DataSet DtSenders, int mailId)
        {
            if (DtSenders.Tables.Count > 0 && DtSenders.Tables[0].Rows.Count > 0)
            {
                DataRow[] drlist = DtSenders.Tables[0].Select("MailId=" + mailId);
                if (drlist.Length > 0)
                {
                    return drlist[0] == null
                               ? "游客"
                               : ("<a href='###' target='_blank'>" +
                                  drlist[0]["CompanyName"] +
                                  "</a>" + "；");
                }
            }
            return "游客";
        }
        #endregion
    }
}
